﻿using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Web.Hosting;
using System.Web.Security;

namespace Facebook.Web
{
    public class FacebookApplication : HttpApplication
    {
        public event EventHandler FacebookContextInit;

        public override void Init()
        {            
            base.Init();

            this.AcquireRequestState += new EventHandler(FacebookApplication_AcquireRequestState);
            this.FacebookContextInit += new EventHandler(FacebookApplication_FacebookContextInit);
        }

        void FacebookApplication_AcquireRequestState(Object sender, EventArgs e)
        {
            if (FacebookApplication.IsProcessableRequest(this.Context.Request.FilePath))
            {
                FacebookWebContext.Init(this.Context);

                if (FacebookWebContext.Current.HasSession)
                {
                    FormsAuthentication.SetAuthCookie(FacebookWebSession.Current.Uid.ToString(), FacebookWebSession.Current.Expires == DateTime.MaxValue);
                }
                else
                {
                    FormsAuthentication.SignOut();
                }
            }
        }

        void FacebookApplication_FacebookContextInit(Object sender, EventArgs e)
        {
            var context = (FacebookWebContext)sender;
            if (this.Application["__FB_FEED_INIT_" + context.ApiKey] == null)
            {
                if (Configuration.FacebookSection.IsConfigured)
                {
                    var config = Configuration.FacebookSection.Current;
                    if (!String.IsNullOrEmpty(Configuration.FacebookSection.Current.FeedTemplateConfigSource))
                    {
                        var templates = Configuration.FeedTemplates.Get(); ;
                        if (templates.Count > 0)
                        {
                            context.Feed.InitFeedTemplates();
                        }
                    }
                }
                this.Application["__FB_FEED_INIT_" + context.ApiKey] = true;
            }
        }

        protected internal void OnFacebookContextInit(Object sender)
        {
            if (this.FacebookContextInit != null)
            {
                this.FacebookContextInit(sender, EventArgs.Empty);
            }
        }

        internal static readonly List<String> ignoredExtensions = new List<String>
        {
            ".css",
            ".jpg",
            ".png",
            ".gif",
            ".htm",
            ".html",
            ".ico",
            ".js"
        };

        internal static Boolean IsProcessableRequest(String virtualPath)
        {
            var file = new FileInfo(HostingEnvironment.MapPath(virtualPath));

            if (String.IsNullOrEmpty(file.Extension)) return true;
            else return !ignoredExtensions.Contains(file.Extension);
        }
    }
}